<template>
  <div class="wrap-cityList" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="botton-wrapper">
            <div class="button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="botton-wrapper" v-for="Citem of hotCities" :key="Citem.id" @click="handleCityClick(Citem.name)">
            <div class="button">{{Citem.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(hitems , key) of cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div class="item border-bottom" v-for="item of hitems" :key="item.id" @click="handleCityClick(item.name)">
            {{item.name}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Becroll from 'better-scroll'
import {mapState, mapMutations} from 'vuex'

export default {
  name: 'cityList',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  methods: {
    handleCityClick (city) {
      // this.$store.dispatch('citychange',city)
      // this.$store.commit('citychange', city)
      this.citychange(city)
      this.$router.push('/')
    },
    ...mapMutations(['citychange'])
  },
  mounted () {
    this.scroll = new Becroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  .border-topbottom
    &:before, &:after
      background-color #ccc

  .wrap-cityList
    overflow hidden
    position: absolute
    top: 1.58rem
    left: 0
    bottom: 0
    right: 0
    .title
      background: #eee
      color: #666
      line-height: 0.54rem
      font-size 0.26rem
      padding-left: 0.2rem
    .button-list
      padding: 0.1rem 0.6rem 0.1rem 0.1rem
      overflow hidden
      .botton-wrapper
        float left
        width: 33.33%
      .button
        text-align: center
        broder-radius: 0.3rem
        padding: 0.1rem 0
        border: 0.02rem solid #ccc
        margin: 0.1rem

  .item-list
    .item
      line-height: 0.76rem
      padding-left: 0.2rem
</style>
